﻿using System.Configuration;
using System.Data.Services;
using System.Data.Services.Common;
using DevExpress.Xpo.DB;
using DevExpress.Xpo.Services;
using Mod = Liekhus.TimeTracker.Module;
using System.Reflection;
using System;
using DevExpress.Xpo;
using DevExpress.Persistent.BaseImpl;

namespace Liekhus.TimeTracker.WebServices
{
    public class TimeTracker : XpoDataService
    {
        public static void InitializeService(DataServiceConfiguration config)
        {
            config.SetEntitySetAccessRule("*", EntitySetRights.All);
            config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
            config.UseVerboseErrors = true;
        }

        public override AutoCreateOption AutoCreateOption
        {
            get { return AutoCreateOption.SchemaAlreadyExists; }
        }

        public override string ConnectionString
        {
            get { return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; }
        }

        protected override void RegisterEntities()
        {
            base.RegisterEntities();

            Assembly assembly = typeof(Mod.Task).Assembly;
            foreach (Type type in assembly.GetTypes())
            {
                if (type.GetInterface("IXPSimpleObject")!=null)
                {
                    RegisterEntity(type);
                }
            }
        }
    }
}
